﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernateDB;

// .dll name has to be the same as the namespace
namespace ECGFilter
{
    // ECG filter
    public class Filter : IFilter
    {
        private const int FILTER_ID = 1004;
        private const string FILTER_VER = "1.0";
        private const string FILTER_DESC =
            "ECG filter.\n" +
            "This filter is used to transfer files.";

        // variable used for BP calculations
        private double sumPQ = 0;
        private double sumHeart = 0;
        private int countPQ = 0;
        private int countHeart = 0;
        private double minPQ = 0;
        private double maxPQ = 0;
        private double minHeart = 0;
        private double maxHeart = 0;

        // server functions
        private ServerASISTSYS.ServerFunctions sf;

        #region IFilter Members

        public int GetID()
        {
            return FILTER_ID;
        }

        public string GetVersion()
        {
            return FILTER_VER;
        }

        public string GetDescription()
        {
            return FILTER_DESC;
        }

        public void Parse(int length, int id, byte[] data)
        {
            try
            {
                string str = "";
                string Date = "";
                string Hour = "";
                double ValuePQ = 0;
                double ValueHeart = 0;

                for (int i = 0; i < length; i++)
                {
                    str = str + (char)data[i];
                }

                Console.WriteLine("Data from ECG sensor: " + str);

                if (str.Length > 0)
                {
                    ValuePQ = Double.Parse(str.Substring(0, 5).Trim());
                    ValueHeart = Double.Parse(str.Substring(5, 6).Trim());
                    Date = str.Substring(11, 11).Trim();
                    Hour = str.Substring(22, 8).Trim();
                }

                //Console.WriteLine("ValuePQ: " + ValuePQ);
                //Console.WriteLine("ValueHeart: " + ValueHeart);
                //Console.WriteLine("Date : " + Date);
                //Console.WriteLine("Hour : " + Hour);

                //ECG caluclations
                //PQ interval calculations
                // min PQ     
                if (ValuePQ < 0.12)
                {
                    minPQ = ValuePQ;
                    sf.SendAlertMessage(sf.CNP, id, "MIN PQ " + minPQ);
                }
                // max PQ
                if (ValuePQ > 0.20)
                {
                    maxPQ = ValuePQ;
                    sf.SendAlertMessage(sf.CNP, id, "MAX PQ " + maxPQ);
                }
                // average PQ
                sumPQ = sumPQ + ValuePQ;
                countPQ++;
                double tempMed = sumPQ / countPQ;

                //Console.WriteLine("minPQ : " + minPQ);
                //Console.WriteLine("maxPQ : " + maxPQ);
                //Console.WriteLine("PQMed : " + tempMed);

                // reset sensors minimum and maximum values.
                if (minPQ != 0)
                {
                    //Console.WriteLine("Valoarea minima PQ: " + minPQ);
                    minPQ = 0;
                }
                if (maxPQ != 0)
                {
                    //Console.WriteLine("Valoarea maxima PQ: " + maxPQ);
                    maxPQ = 0;
                }

                //P wave rate, Heart rate calculations
                // min Hart rate    
                if (ValueHeart < 60)
                {
                    minHeart = ValueHeart;
                    sf.SendAlertMessage(sf.CNP, id, "MIN HEART " + minHeart);
                }
                // max Hart rate
                if (ValueHeart > 100)
                {
                    maxHeart = ValueHeart;
                    sf.SendAlertMessage(sf.CNP, id, "MAX HEART " + maxHeart);
                }
                // average Hart rate
                sumHeart = sumHeart + ValueHeart;
                countHeart++;
                double tempMed1 = sumHeart / countHeart;

                //Console.WriteLine("minHeart : " + minHeart);
                //Console.WriteLine("maxHeart : " + maxHeart);
                //Console.WriteLine("HeartMed : " + tempMed1);

                // reset sensors minimum and maximum values.
                if (minHeart > 0)
                {
                    //Console.WriteLine("Valoarea minima Heart: " + minHeart);
                    minHeart = 0;
                }
                if (maxHeart > 0)
                {
                    //Console.WriteLine("Valoarea maxima Heart: " + maxHeart);
                    maxHeart = 0;
                }

                // adaugare la BD
                SenzoriECG secg = new SenzoriECG();

                secg.IdSenzor = id;
                secg.IdPacient = sf.CNP;
                secg.PQ = ValuePQ;
                secg.Heart = ValueHeart;
                secg.Data = DateTime.Parse(Date + " " + Hour);

                PersistenceManager pm = PersistenceManager.Instance;
                pm.Save(secg);
            }
            catch (FormatException)
            {
                // the message could not be processed
                Console.WriteLine("The format of the string is incorect!");
            }
        }

        public bool Validate()
        {
            return true;
            //throw new NotImplementedException();
        }

        public void SetServerFunctions(object sf)
        {
            this.sf = (ServerASISTSYS.ServerFunctions)sf;
        }

        public bool CanProcess(int id)
        {
            switch (id)
            {
                case 300001:
                    return true;
                default:
                    return false;
            }
        }

        #endregion
    }
}
